# -*- coding: utf-8 -*-
# @ Time   :  2020/7/24 17:26
# @ Author : Redtree
# @ File :  game.py
# @ Desc :  游戏核心业务代码


import configparser #配置读取工具
import pygame #游戏引擎
import db #基础数据-静态
import dr #游戏动态数据
import sys #python系统组件
import copy #数据深度拷贝工具
import random #随机算法库
import aif #AI相关算法

#点击开始游戏的事件
def start_game_event(vsmode):
   #重置音乐
   pygame.mixer.music.stop()
   pygame.mixer.music.load('mus/battle2.mp3')
   #初始化地图数据
   imap=[]
   for x in range(0,15):
       heng = []
       for y in range(0,15):
           heng.append(0)
       imap.append(heng)
   rtgd['p1map']=copy.deepcopy(imap)
   rtgd['p2map']=copy.deepcopy(imap)
   #直接进入阶段二的调试
   rtgd['p1bfm'] = copy.deepcopy(rtgd['p2map'])
   rtgd['p2bfm'] = copy.deepcopy(rtgd['p1map'])
   #重置数据，后面写个方法等数据确定下来
   rtgd['s1p1c']=0
   rtgd['s1p1bx']=1
   rtgd['s2p2c']=0
   rtgd['s2p2bx']=1
   rtgd['irs']=False
   rtgd['bf1x']=0
   rtgd['bf2x']=512
   rtgd['p1blt']=1
   rtgd['p2blt']=1
   rtgd['p1dsc']=[]
   rtgd['p2dsc']=[]
   rtgd['p1sps']=[]
   rtgd['p2sps']=[]
   rtgd['iss']=False
   rtgd['lbp']=[]
   rtgd['bflying']=False
   rtgd['fly_speed']=20
   rtgd['tostart_speed']=20
   rtgd['scored']=False
   rtgd['hua']=0
   rtgd['lst']=0
   rtgd['huasps']=[]
   rtgd['huatime']=0
   rtgd['victory']='baba'
   rtgd['pstage']=0
   rtgd['vsmode']=vsmode
   rtgd['aic']=[0,0,0]
   #切换场景
   rtgd['cur_scene'] = db.SCENE_STATUS['game']['code']


#获取存活船只表
def get_alive_cs(cbfm):
    clist=[]
    for aa in rtgd[cbfm]:
        for a in aa:
            if a>0 and not a in clist:
                clist.append(a)
    return clist


#绘制战斗海图
def draw_map_battle(cbfm,cmap):
    clist = get_alive_cs(cbfm)
    mx = 0
    my = 0
    for heng in rtgd[cbfm]:
        for h in heng:
            cres = dres.SEA
            if h>=0:
                cres = dres.SEA
            elif h==-1:
                if rtgd[cmap][my][mx] in clist:
                    cres = dres.FIRE
                else:
                    cres = dres.BFIRE
            elif h==-2:
                cres = dres.SMOKE
            screen.blit(cres, (212 + mx * 40, 168 + my * 40))
            mx += 1
            if mx > 14:
                mx = 0
                my += 1


#绘制地形海洋
def draw_map_sea(cmap):
    # 地图
    mx = 0
    my = 0
    for heng in rtgd[cmap]:
        for h in heng:
            screen.blit(dres.SEA, (212 + mx * 40, 168 + my * 40))
            mx += 1
            if mx > 14:
                mx = 0
                my += 1


def draw_map_ships(player_map):
    for ck in range(1, 6):
        for dy in range(0, 15):
            asp = False  # 是否找到锚点
            for dx in range(0, 15):
                cms = rtgd[player_map][dy][dx]
                if cms == ck:
                    # 检测横向还是竖向
                    if cms==1:
                        screen.blit(dres.FCS[cms - 1], (212 + dx * 40, 168 + dy * 40))
                    elif cms == rtgd[player_map][dy][dx+1]:
                        rtfcs = pygame.transform.rotate(dres.FCS[cms - 1], 90)
                        screen.blit(rtfcs, (212 + dx * 40, 168 + dy * 40))
                    else:
                        # 竖向
                        screen.blit(dres.FCS[cms - 1], (212 + dx * 40, 168 + dy * 40))
                    asp = True
                    break
            if asp == True:
                break


def check_could_set(ix,iy,mx,my):
    #print(ix,iy)
    if rtgd[spc] == 1:
        if rtgd[cmap][iy][ix] == 0:
            rtgd['irs'] = True
    elif rtgd[spc] == 2:
        if rtgd[sbx] == 1 and my <=688 :
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy + 1][ix] == 0:
                rtgd['irs'] = True
        elif rtgd[sbx] == 0 and mx <= 732:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy][ix + 1] == 0:
                rtgd['irs'] = True
    elif rtgd[spc] == 3:
        if rtgd[sbx] == 1 and my <= 648:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy + 1][ix] == 0 and rtgd[cmap][iy + 2][ix] == 0:
                rtgd['irs'] = True
        elif rtgd[sbx] == 0 and mx <= 692:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy][ix + 1] == 0 and rtgd[cmap][iy + 2][ix + 2] == 0:
                rtgd['irs'] = True
    elif rtgd[spc] == 4:
        if rtgd[sbx] == 1 and my <= 608:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy + 1][ix] == 0 and rtgd[cmap][iy + 2][ix] == 0 and \
                    rtgd[cmap][iy + 3][ix] == 0:
                rtgd['irs'] = True
        elif rtgd[sbx] == 0 and mx <= 652:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy][ix + 1] == 0 and rtgd[cmap][iy][ix + 2] == 0 and \
                    rtgd[cmap][iy][ix + 3] == 0:
                rtgd['irs'] = True
    elif rtgd[spc] == 5:
        if rtgd[sbx] == 1 and my <= 568:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy + 1][ix] == 0 and rtgd[cmap][iy + 2][ix] == 0 and \
                    rtgd[cmap][iy + 3][ix] == 0 and rtgd[cmap][iy + 4][ix] == 0:
                rtgd['irs'] = True
        elif rtgd[sbx] == 0 and mx <= 612:
            if rtgd[cmap][iy][ix] == 0 and rtgd[cmap][iy][ix + 1] == 0 and rtgd[cmap][iy][ix + 2] == 0 and \
                    rtgd[cmap][iy][ix + 3] == 0 and rtgd[cmap][iy][ix + 4] == 0:
                rtgd['irs'] = True


#校验地图上是否有部署某船只
def check_one_setover(player,boart):
    if player=='baba':
        cmap = rtgd['p1map']
    else:
        cmap = rtgd['p2map']
    for cm in cmap:
        for c in cm:
            if c==boart:
                return True
    return False

#确认是否全部部署
def check_all_setover(player):
    if player=='baba':
        cmap = rtgd['p1map']
    else:
        cmap = rtgd['p2map']
    cl = [0,0,0,0,0]
    for cm in cmap:
        for c in cm:
            if c==1:
                cl[0]=1
            elif c==2:
                cl[1]=1
            elif c == 3:
                cl[2]=1
            elif c ==4:
                cl[3] = 1
            elif c == 5:
                cl[4] = 1
    if 0 in cl:
        return False
    return True


#显示强化炮弹类型
def draw_bullet_type(blt):
    #文字
    screen.blit(dres.HELPTS[rtgd[blt]-1],(20,200))
    #blt_img
    screen.blit(dres.BLT, (70, 250))


#鼠标绘制
def draw_mouse():
    if rtgd['pstage']==0:
        spc='s1p1c'
    else:
        spc='s2p2c'
    # 鼠标最后画
    if x >= 212 and x < 812 and y >= 168 and y < 768 and rtgd['pstage'] in [0,1] and rtgd['cur_scene']=='game' and rtgd[spc]>0:
        pass
    elif x >= 212 and x < 812 and y >= 168 and y < 768 and rtgd['pstage'] in [2,3] and rtgd['cur_scene']=='game':
        pass
    else:
        screen.blit(dres.MOUSE, (x - 18, y - 18))


def rot_center(image, angle):
    """rotate an image while keeping its center and size"""
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image


if __name__ == "__main__":
    #加载游戏基础配置
    cfg_path = 'config.ini'
    conf = configparser.ConfigParser()
    conf.read(cfg_path)
    FULL_SCREEN = int(conf.get('Graphics','fullscreen'))
    WEIGHT =  int(conf.get('Graphics','weight'))
    HEIGHT =  int(conf.get('Graphics','height'))
    WINDOW_SIZE = WEIGHT,HEIGHT
    #初始化游戏引擎
    pygame.init()
    #设置窗口标题
    pygame.display.set_caption(db.GAME_NAME + ':' + db.VERSION)
    import dres #游戏素材资源管理工具，必须在pygame.init()后加入
    pygame.display.set_icon(dres.ANGRY)
    #设置窗口模式
    if FULL_SCREEN==1:
        screen = pygame.display.set_mode(WINDOW_SIZE,pygame.FULLSCREEN,32)
    else:
        screen = pygame.display.set_mode(WINDOW_SIZE,pygame.RESIZABLE)
    #设置帧数
    FPS=60
    Clock=pygame.time.Clock()
    Clock.tick(FPS)
    #隐藏鼠标
    pygame.mouse.set_visible(False)
    #实时游戏数据
    rtgd = copy.deepcopy(dr.DATA)
    #音乐
    pygame.mixer.init()
    pygame.time.delay(1000)  # 等待1秒让mixer完成初始化
    pygame.mixer.music.load("mus/title.mp3")
    #音效
    sound_kaipao = pygame.mixer.Sound("mus/kaipao.wav")
    sound_guaishou = pygame.mixer.Sound("mus/guaishou.wav")
    sound_victory = pygame.mixer.Sound("mus/victory.wav")
    #游戏业务逻辑
    while True:
        # 监听鼠标点位置
        if rtgd['pstage']==3 and rtgd['vsmode']==0:
            x,y = rtgd['aic'][0],rtgd['aic'][1]
        else:
            x, y = pygame.mouse.get_pos()

        is_mouse_down=False
        is_mouse_right_down=False
        #screen.fill(sbd.COLOR_RGB_BLACK)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print('退出游戏')
                sys.exit()
            if rtgd['pstage']==3 and rtgd['vsmode']==0:
                is_mouse_down=rtgd['aic'][2]
            else:
                if event.type == pygame.MOUSEBUTTONDOWN :
                    if event.button==1:
                        is_mouse_down = True
                    else:
                        is_mouse_down = False
                    if event.button==3:
                        is_mouse_right_down=True
                    else:
                        is_mouse_right_down=False


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
        #持续按住的事件处理
        # key_pressed = pygame.key.get_pressed()
        # if key_pressed[pygame.K_w]:
        #     rtgd['keys']['w'] = 1
        # else:
        #     rtgd['keys']['w'] = 0
        # if key_pressed[pygame.K_a]:
        #     rtgd['keys']['a'] = 1
        # else:
        #     rtgd['keys']['a'] = 0
        # if key_pressed[pygame.K_s]:
        #     rtgd['keys']['s'] = 1
        # else:
        #     rtgd['keys']['s'] = 0
        # if key_pressed[pygame.K_d]:
        #     rtgd['keys']['d'] = 1
        # else:
        #     rtgd['keys']['d'] = 0
        if rtgd['cur_scene'] == db.SCENE_STATUS['title']['code']:
            # 检查音乐流播放，有返回True，没有返回False
            # 如果没有音乐流则选择播放
            if pygame.mixer.music.get_busy() == False:
               pygame.mixer.music.play(-1,0)
            screen.blit(dres.TITLE, (0, 0))
            x0, y0 = int(WEIGHT / 2.8), int(HEIGHT / 3)
            x1, y1 = int(WEIGHT / 2.8), int(HEIGHT / 3)+60
            x2, y2 = int(WEIGHT / 2.8), int(HEIGHT / 3)+ 120
            x3, y3 =int(WEIGHT / 2.8), int(HEIGHT / 3)+ 180
            if x1 + 160 > x >= x1 and y1 + 40 >= y >= y1:
                screen.blit(dres.TITLE_computer_surface_pos, (x1, y1))
                if is_mouse_down == True:
                    start_game_event(0)
            else:
                screen.blit(dres.TITLE_computer_surface, (x1, y1))
            if x0 + 160 > x >= x0 and y0 + 40 >= y >= y0 :
                screen.blit(dres.TITLE_player_surface_pos, (x0, y0))
                if is_mouse_down == True:
                    start_game_event(1)
            else:
                screen.blit(dres.TITLE_player_surface, (x0, y0))
            if x2 + 160 > x >= x2 and y2 + 40 >= y >= y2:
                screen.blit(dres.TITLE_law_surface_pos, (x2, y2))
            else:
                screen.blit(dres.TITLE_law_surface, (x2, y2))
            if x3 + 160 > x >= x3 and y3 + 40 >= y >= y3:
                screen.blit(dres.TITLE_exit_surface_pos, (x3, y3))
                if is_mouse_down == True:
                    sys.exit()
            else:
                screen.blit(dres.TITLE_exit_surface, (x3, y3))
        elif rtgd['cur_scene'] == db.SCENE_STATUS['victory']['code']:
            if rtgd['victory']=='baba':
                screen.blit(dres.VICTORY_BABA,(0,0))
            else:
                screen.blit(dres.VICTORY_MAMA,(0,0))
            #重开和退出按钮
            if x>=472 and x<552 and y>=364 and y<404:
                cres = pygame.transform.scale(dres.RESTART,(100,60))
                screen.blit(cres,(472,364))
                if is_mouse_down==True:
                    #重开逻辑
                    start_game_event(rtgd['vsmode'])
            else:
                screen.blit(dres.RESTART,(472,364))
            if x>=472 and x<552 and y>=424 and y<464:
                cres = pygame.transform.scale(dres.BT_EXIT,(100,60))
                screen.blit(cres,(472,424))
                if is_mouse_down==True:
                    #返回主菜单
                    rtgd['cur_scene'] = db.SCENE_STATUS['title']['code']
            else:
                screen.blit(dres.BT_EXIT,(472,424))

        elif rtgd['cur_scene'] == db.SCENE_STATUS['game']['code']:
            #背景填充
            screen.fill(db.COLOR_RGB_GRAY)
            #根据游戏阶段渲染面板
            #玩家一部署阶段
            if rtgd['pstage'] in [0,1]:
                if pygame.mixer.music.get_busy() == False:
                    pygame.mixer.music.play(-1, 0)
                #读取基础变量
                SPT = dres.SPT1
                check_name = 'baba'
                spc = 's1p1c'
                cmap = 'p1map'
                sbx='s1p1bx'
                if rtgd['pstage'] == 1:
                    SPT = dres.SPT2
                    check_name = 'mama'
                    spc = 's2p2c'
                    cmap = 'p2map'
                    sbx = 's2p2bx'
                #面板
                screen.blit(dres.PHB, (0, 0))
                screen.blit(dres.P2B, (812, 168))
                screen.blit(dres.P1B, (0, 168))
                screen.blit(dres.PCB, (0, 168))
                screen.blit(dres.PCB, (812, 168))
                #玩家头像
                if rtgd['pstage']==0:
                    screen.blit(dres.BABA,(0,0))
                else:
                    screen.blit(dres.MAMA,(0,0))
                #对话气泡
                screen.blit(dres.TALK,(215,20))
                #绘制地图海洋
                draw_map_sea(cmap)
                # 绘制已部署的船只
                draw_map_ships(cmap)
                #鼠标选中左侧船只
                if x >= 0 and x <= 212:
                    if y >= 168 and y < 208:
                        # 选中水怪
                        pygame.draw.rect(screen, db.COLOR_RGB_YELLOW, [0, 168, 212, 40], 0)
                        if is_mouse_down == True and check_one_setover(check_name,1)==False:
                            rtgd[spc] = 1
                    elif y >= 208 and y < 288:
                        # 选中二级船
                        pygame.draw.rect(screen, db.COLOR_RGB_YELLOW, [0, 208, 212, 80], 0)
                        if is_mouse_down == True and check_one_setover(check_name,2)==False:
                            rtgd[spc] = 2
                    elif y >= 288 and y < 408:
                        # 选中三级船
                        pygame.draw.rect(screen, db.COLOR_RGB_YELLOW, [0, 288, 212, 120], 0)
                        if is_mouse_down == True and check_one_setover(check_name,3)==False:
                            rtgd[spc] = 3
                    elif y >= 408 and y < 568:
                        # 选中四级船
                        pygame.draw.rect(screen, db.COLOR_RGB_YELLOW, [0, 408, 212, 160], 0)
                        if is_mouse_down == True and check_one_setover(check_name,4)==False:
                            rtgd[spc] = 4
                    elif y >= 568:
                        # 选中五级船
                        pygame.draw.rect(screen, db.COLOR_RGB_YELLOW, [0, 568, 212, 200], 0)
                        if is_mouse_down == True and check_one_setover(check_name,5)==False:
                            rtgd[spc] = 5
                # 左侧船只标识
                zmy = 168
                for n in range(0, 5):
                    zmy = zmy + n * 40
                    if rtgd[spc] != n + 1 and check_one_setover(check_name,n+1)==False:
                        screen.blit(dres.FCS[n], (40, zmy))
                #当鼠标位于战场内部
                if x>=212 and x<812 and y>=168 and y<768:
                    #重新选择船只
                    if rtgd[spc]==0:
                        #判断是否重新选择船只,若需要则重新拾起，清除战场数据,这个做完后才是完成部署
                        px = x-212
                        py = y-168
                        if rtgd[cmap][py//40][px//40]>0 and is_mouse_down==True:
                            #检查当前摆向
                            rtgd[spc] = rtgd[cmap][py // 40][px // 40]
                            if rtgd[cmap][py//40][px//40+1]== rtgd[cmap][py//40][px//40]:
                                  rtgd[sbx]=0
                            else:
                                 rtgd[sbx] =1
                            # 清除棋盘指定船只
                            pdy=0
                            for ps in rtgd[cmap]:
                                pdx = 0
                                for p in ps:
                                    if p==rtgd[spc]:
                                        rtgd[cmap][pdy][pdx]=0
                                    pdx+=1
                                pdy+=1
                        #战场内空闲对话
                        screen.blit(SPT[0], (320, 40))
                        screen.blit(SPT[1], (320, 66))
                        screen.blit(SPT[2], (300, 92))
                    else:
                        #点击右键调整船体摆向
                        if is_mouse_right_down==1 and rtgd[spc]>1:
                            if rtgd[sbx] == 1:
                                rtgd[sbx]=0
                            else:
                                rtgd[sbx]=1
                        # 位置矫正
                        mx=0
                        for nx in db.MXS:
                            if 0<=x-nx<40:
                                mx=nx
                                break
                        my = 0
                        for ny in db.MYS:
                            if 0<=y-ny <40:
                                my = ny
                                break
                        '''
                        MXS=[212, 252, 292, 332, 372, 412, 452, 492, 532, 572, 612, 652, 692, 732, 772]
                        MYS=[168, 208, 248, 288, 328, 368, 408, 448, 488, 528, 568, 608, 648, 688, 728]
                        '''
                        #船体调整
                        if rtgd[spc]>0:
                            if rtgd[sbx]==1:
                                screen.blit(dres.FCS[rtgd[spc] - 1], (mx, my))
                            else:
                                rtfcs = pygame.transform.rotate(dres.FCS[rtgd[spc] - 1],90)
                                screen.blit(rtfcs,(mx,my))

                            #is right set?
                            rtgd['irs']=False
                            #部署确认和部署规则检索 (查询战场矩阵坐标)
                            ix = 0
                            iy = 0
                            for cx in db.MXS:
                                if cx == mx:
                                    break
                                ix += 1
                                if ix>14:
                                      ix=14
                            for cy in db.MYS:
                                if cy == my:
                                    break
                                iy += 1
                                if iy>14:
                                    iy=14
                            check_could_set(ix,iy,mx,my)
                            if rtgd['irs']==True:
                                # 对话内容
                                screen.blit(SPT[4], (320, 66))
                                #根据不同船只触发对话
                                #可部署状态下，左键部署
                                if is_mouse_down==True:
                                    #更新矩阵
                                    if rtgd[sbx]==1:
                                        for upr in range(0,rtgd[spc]):
                                            rtgd[cmap][iy+upr][ix]=rtgd[spc]
                                    else:
                                        for upr in range(0,rtgd[spc]):
                                            rtgd[cmap][iy][ix+upr]=rtgd[spc]
                                    #重置选中
                                    rtgd[spc]=0
                                    rtgd[sbx]=1
                            else:
                                screen.blit(SPT[3], (320, 66))
                else:
                    # 鼠标位于结束部署按钮
                    if x >= 860 and x < 940 and ((y >= 95 and y < 135) or(y >= 25 and y < 65) ):
                        #结束对话内容
                        pass
                    else:
                        screen.blit(dres.BT_EXIT, (860, 95))
                        # 对话内容
                        screen.blit(SPT[0], (320, 40))
                        screen.blit(SPT[1], (320, 66))
                        screen.blit(SPT[2], (300, 92))
                # 鼠标位于结束部署按钮
                if x >= 860 and x < 940 and y >= 95 and y < 135:
                    cur_bt_exit = pygame.transform.scale(dres.BT_EXIT, (100, 60))
                    screen.blit(cur_bt_exit, (860, 95))
                    screen.blit(SPT[5], (320, 66))
                    if is_mouse_down == True:
                        # 回到菜单
                        rtgd['cur_scene'] = db.SCENE_STATUS['title']['code']
                else:
                    screen.blit(dres.BT_EXIT, (860, 95))
                #如果部署完毕，出现部署完成按钮
                if check_all_setover(check_name)==True:
                    if x >= 860 and x < 940 and y >= 25 and y < 65:
                        cur_bt_setover = pygame.transform.scale(dres.BT_SETOVER, (100, 60))
                        screen.blit(cur_bt_setover, (860, 25))
                        screen.blit(SPT[6], (320, 66))
                        if is_mouse_down == True:
                            # 回到菜单
                            if rtgd['vsmode']==1:
                                rtgd['pstage'] +=1
                                if rtgd['pstage']==2:
                                    # 重置音乐
                                    pygame.mixer.music.stop()
                                    pygame.mixer.music.load('mus/battle1.mp3')
                                    rtgd['bf1x']=0
                                    rtgd['bf2x']=512
                                    rtgd['p1bfm'] = copy.deepcopy(rtgd['p2map'])
                                    rtgd['p2bfm'] = copy.deepcopy(rtgd['p1map'])
                            else:
                                if rtgd['pstage']==0:
                                    rtgd['pstage'] += 2
                                    # 重置音乐
                                    pygame.mixer.music.stop()
                                    pygame.mixer.music.load('mus/battle1.mp3')
                                    rtgd['bf1x'] = 0
                                    rtgd['bf2x'] = 512
                                    rtgd['p2map'] = copy.deepcopy(aif.create_ai_map())
                                    rtgd['p1bfm'] = copy.deepcopy(rtgd['p2map'])
                                    rtgd['p2bfm'] = copy.deepcopy(rtgd['p1map'])
                    else:
                        screen.blit(dres.BT_SETOVER, (860, 25))
            #进入战斗
            elif rtgd['pstage'] in [2,3]:
                if pygame.mixer.music.get_busy() == False:
                    pygame.mixer.music.play(-1, 0)
                # 读取基础变量
                check_name = 'baba'
                cmap = 'p2map'
                cbfm = 'p1bfm'
                sptf = dres.SPT1F
                #火炮形态
                blt ='p1blt'
                #已摧毁船只记录
                dsc = 'p1dsc'
                #本轮射击坐标组
                sps = 'p1sps'
                if rtgd['pstage'] == 3:
                    check_name = 'mama'
                    cmap = 'p1map'
                    cbfm = 'p2bfm'
                    sptf = dres.SPT2F
                    blt = 'p2blt'
                    dsc = 'p2dsc'
                    sps = 'p2sps'
                #胜利校验
                clist = get_alive_cs(cbfm)
                if len(clist)==1 and 1 in clist:
                    #跳转胜利界面
                    rtgd['victory']=check_name
                    rtgd['cur_scene'] = db.SCENE_STATUS['victory']['code']
                    #播放胜利音乐
                    sound_victory.play(0)
                else:
                    #游戏继续
                    # 面板
                    screen.blit(dres.PHB, (0, 0))
                    screen.blit(dres.P2B, (812, 168))
                    screen.blit(dres.P1B, (0, 168))
                    screen.blit(dres.PCB, (0, 168))
                    screen.blit(dres.PCB, (812, 168))
                    # 玩家头像
                    if rtgd['pstage'] == 2:
                        screen.blit(dres.BABA, (0, 0))
                    else:
                        screen.blit(dres.MAMA, (0, 0))
                    # 对话气泡
                    screen.blit(dres.TALK, (215, 20))
                    # 绘制地图海洋
                    draw_map_sea(cmap)
                    # 绘制敌方部署
                    draw_map_ships(cmap)
                    # 绘制我方战斗覆盖
                    draw_map_battle(cbfm,cmap)
                    #显示当前炮弹类型
                    draw_bullet_type(blt)

                    #退出按钮
                    if x>=860 and x<940 and y>=95 and y<135:
                        cres = pygame.transform.scale(dres.BT_EXIT,(100,60))
                        screen.blit(cres, (860, 95))
                        screen.blit(sptf[3], (360, 70))
                        if is_mouse_down==True:
                            #回到菜单
                            rtgd['cur_scene'] = db.SCENE_STATUS['title']['code']
                    else:
                        screen.blit(dres.BT_EXIT, (860, 95))
                        # 基础对话
                        screen.blit(sptf[0], (360, 40))
                        screen.blit(sptf[1], (350, 66))
                        screen.blit(sptf[2], (340, 92))
                    #绘制右侧船只标识,若被摧毁则打上印记
                    yc = 168
                    clist =get_alive_cs(cbfm)
                    for n in range(0, 5):
                        yc = yc + n * 40
                        screen.blit(dres.FCS[n], (892, yc))
                        if not n+1 in clist:
                            screen.blit(dres.DESTROY,(872,yc+n*20))
                    #开炮后随机方向进入放炮动画
                    if rtgd['bf1x']>-512 and rtgd['bf2x']<1024:
                        # 过场动画BATTLE,先简单点
                        screen.blit(dres.BATTLE1, (rtgd['bf1x'], 0))
                        screen.blit(dres.BATTLE2, (rtgd['bf2x'], 0))
                        #拉序幕速度
                        rtgd['bf1x']-=rtgd['tostart_speed']
                        rtgd['bf2x']+=rtgd['tostart_speed']
                    elif rtgd['bflying']==True:
                        #让子弹飞,位置移动
                        all_set = True
                        for cp in rtgd['lbp']:
                            if abs(cp[2]-cp[0])>=abs(cp[3]-cp[1]):
                                if cp[2]>cp[0]:
                                    if cp[2]-cp[0]>=rtgd['fly_speed']:
                                        cp[2]-=rtgd['fly_speed']
                                    else:
                                        cp[2]=cp[0]
                                    all_set=False
                                elif cp[0]>cp[2]:
                                    if cp[0]-cp[2]>=rtgd['fly_speed']:
                                        cp[2]+=rtgd['fly_speed']
                                    else:
                                        cp[2]=cp[0]
                                    all_set=False
                            if abs(cp[2]-cp[0])<=abs(cp[3]-cp[1]):
                                if cp[3]>cp[1]:
                                    if cp[3]-cp[1]>=rtgd['fly_speed']:
                                        cp[3]-=rtgd['fly_speed']
                                    else:
                                        cp[3]=cp[1]
                                    all_set=False
                                elif cp[1]>cp[3]:
                                    if cp[1]-cp[3]>=rtgd['fly_speed']:
                                        cp[3]+=rtgd['fly_speed']
                                    else:
                                        cp[3]=cp[1]
                                    all_set = False
                            if cp[3]==cp[1] and cp[2]==cp[0]:
                                #尝试单格结算
                                if rtgd[cbfm][cp[5]][cp[4]]>1:
                                    #后面对划水怪单独处理
                                    rtgd[cbfm][cp[5]][cp[4]]=-1
                                    screen.blit(dres.FIRE, (cp[0], cp[1]))
                                    rtgd['scored']=True
                                elif rtgd[cbfm][cp[5]][cp[4]]==1:
                                    rtgd[cbfm][cp[5]][cp[4]]=-1
                                    screen.blit(dres.FIRE, (cp[0], cp[1]))
                                    sound_guaishou.play(0)
                                    if check_name=='baba':
                                        rtgd['hua']=1
                                        rtgd['huasps']=[]
                                        rtgd['huatime']=40
                                    else:
                                        rtgd['hua']=2
                                        rtgd['huasps']=[]
                                        rtgd['huatime']=40
                                elif rtgd[cbfm][cp[5]][cp[4]]==-1:
                                    pass
                                else:
                                    rtgd[cbfm][cp[5]][cp[4]]=-2
                                    screen.blit(dres.SMOKE, (cp[0], cp[1]))
                            else:
                                bres = pygame.transform.rotate(dres.BULLET, cp[2])
                                screen.blit(bres, (cp[2], cp[3]))
                        if all_set==True:
                            # (结算地图数据,若有命中或击毁则继续，无则下一轮)、
                            # (换手逻辑)、(升级炮弹逻辑和(划水怪逻辑))\
                            # (一轮打击完毕后，确认是否有新摧毁船只)
                            # （退出按钮）
                            # （毁灭船只展示）、
                            #  (destroy位置不对，优先改)
                            # (看得清哪里有船(bug,优先改))
                            # (升级炮弹说明)、（划水怪）、(战斗中是否出现重开)、（胜负逻辑）
                            # (停滞时间，连续命中缩短，提高用户体验)
                            ctime=60
                            if rtgd['scored'] == True and rtgd['hua']==0:
                                ctime= 5
                            if rtgd['lst']<ctime:
                                rtgd['lst']+=1
                                if ctime==60:
                                    #缓冲小可爱图像（待定）
                                    screen.blit(dres.HEMA,(20,400+rtgd['lst']))
                            else:
                                rtgd['lst']=0
                                get_alive_cs(cbfm)
                                clist = get_alive_cs(cbfm)
                                ckl = [2, 3, 4, 5]
                                up_bullet_flag = False
                                for c in ckl:
                                    if not c in clist and not c in rtgd[dsc]:
                                        rtgd[dsc].append(c)
                                        up_bullet_flag = True
                                if up_bullet_flag == True:
                                    # 升级当前用户的炮弹,打一炮后取消
                                    rtgd[blt] = random.randint(2, 7)
                                else:
                                    rtgd[blt] = 1
                                rtgd['bflying'] = False
                                if rtgd['scored'] == False:
                                    if rtgd['pstage'] == 2:
                                        rtgd['pstage'] = 3
                                    else:
                                        rtgd['pstage'] = 2
                                else:
                                    if rtgd['hua'] > 0:
                                        if rtgd['pstage'] == 2:
                                            rtgd['pstage'] = 3
                                        else:
                                            rtgd['pstage'] = 2
                    elif rtgd['hua'] > 0:
                        # 划水怪攻击事件,随机向对方发射5枚炮弹，但不触发连机
                        # 先放个愤怒章鱼动画,再分配攻击点，动画、结算。
                        if rtgd['huatime']>0:
                            bl = int(200*(abs(rtgd['huatime']-40)/20))
                            cres = pygame.transform.scale(dres.ANGRY,(bl,bl))
                            adp = int(abs(rtgd['huatime']-40)*5)
                            screen.blit(cres,(512-adp,484-adp))
                            rtgd['huatime']-=1
                        else:
                            if rtgd['huasps']==[]:
                                #分配完攻击位置
                                while len(rtgd['huasps'])<5:
                                    sound_kaipao.play(0)
                                    curx=random.randint(0,14)
                                    cury=random.randint(0,14)
                                    if not [curx,cury] in rtgd['huasps']:
                                        rtgd['huasps'].append([curx,cury])
                                # 随机生成对应的炮弹发射位置
                                rtgd['lbp'] = []
                                for cpi in rtgd['huasps']:
                                    cmx = db.MXS[cpi[0]]
                                    cmy = db.MYS[cpi[1]]
                                    rtgd['lbp'].append(
                                        [cmx, cmy, cmx + random.randint(-800, 800), cmy + random.randint(-500, 500),
                                         cpi[0], cpi[1]])
                                # 2修改状态
                                rtgd['scored'] = False
                            else:
                                # 让子弹飞,位置移动
                                all_set = True
                                for cp in rtgd['lbp']:
                                    if abs(cp[2] - cp[0]) >= abs(cp[3] - cp[1]):
                                        if cp[2] > cp[0]:
                                            if cp[2] - cp[0] >= rtgd['fly_speed']:
                                                cp[2] -= rtgd['fly_speed']
                                            else:
                                                cp[2] = cp[0]
                                            all_set = False
                                        elif cp[0] > cp[2]:
                                            if cp[0] - cp[2] >= rtgd['fly_speed']:
                                                cp[2] += rtgd['fly_speed']
                                            else:
                                                cp[2] = cp[0]
                                            all_set = False
                                    if abs(cp[2] - cp[0]) <= abs(cp[3] - cp[1]):
                                        if cp[3] > cp[1]:
                                            if cp[3] - cp[1] >= rtgd['fly_speed']:
                                                cp[3] -= rtgd['fly_speed']
                                            else:
                                                cp[3] = cp[1]
                                            all_set = False
                                        elif cp[1] > cp[3]:
                                            if cp[1] - cp[3] >= rtgd['fly_speed']:
                                                cp[3] += rtgd['fly_speed']
                                            else:
                                                cp[3] = cp[1]
                                            all_set = False
                                    if cp[3] == cp[1] and cp[2] == cp[0]:
                                        # 尝试单格结算
                                        if rtgd[cbfm][cp[5]][cp[4]] > 1:
                                            # 后面对划水怪单独处理
                                            rtgd[cbfm][cp[5]][cp[4]] = -1
                                            screen.blit(dres.FIRE, (cp[0], cp[1]))
                                            rtgd['scored'] = True
                                        elif rtgd[cbfm][cp[5]][cp[4]] == 1:
                                            rtgd[cbfm][cp[5]][cp[4]] = -1
                                            screen.blit(dres.FIRE, (cp[0], cp[1]))
                                            #水怪攻击不触发任何效果
                                        elif rtgd[cbfm][cp[5]][cp[4]] == -1:
                                            pass
                                        else:
                                            rtgd[cbfm][cp[5]][cp[4]] = -2
                                            screen.blit(dres.SMOKE, (cp[0], cp[1]))
                                    else:
                                        bres = pygame.transform.rotate(dres.BULLET, cp[2])
                                        screen.blit(bres, (cp[2], cp[3]))
                                if all_set == True:
                                        rtgd['hua'] = 0
                    else:
                        #战斗交互开始
                        #校验鼠标所属的开炮位置(鼠标位于战场内)
                        if rtgd['pstage']==3 and rtgd['vsmode']==0:
                            aizxy = aif.aim_shoot_spxy(rtgd[cbfm])
                            x=db.MXS[aizxy[0]]
                            y=db.MXS[aizxy[1]]
                            is_mouse_down=True

                        if x >= 212 and x < 812 and y >= 168 and y < 768:
                            # 位置矫正
                            mx = 0
                            for nx in db.MXS:
                                if   0 <=x - nx < 40:
                                    mx = nx
                                    break
                            my = 0
                            for ny in db.MYS:
                                if  0<=y - ny <40:
                                    my = ny
                                    break
                            #检索射击坐标
                            ix = 0
                            iy = 0
                            for cx in db.MXS:
                                if cx == mx:
                                    break
                                ix += 1
                                if ix>14:
                                   ix=14
                            for cy in db.MYS:
                                if cy == my:
                                    break
                                iy += 1
                                if iy>14:
                                    iy=14
                            #测试火炮
                            #rtgd[blt]=7
                            #能否炮击
                            rtgd['iss']=True
                            #检索炮弹形态,绘制瞄准镜,替换鼠标
                            if rtgd[blt]==1:
                                #单
                                screen.blit(dres.STAR, (mx, my))
                                rtgd[sps]=[[ix,iy]]
                            elif rtgd[blt]==2 and ix<13:
                                #横三
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx+40, my))
                                screen.blit(dres.STAR, (mx+80, my))
                                rtgd[sps] = [[ix, iy],[ix+1, iy],[ix+2, iy]]
                            elif rtgd[blt]==3 and iy<13:
                                #竖三
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx , my+40))
                                screen.blit(dres.STAR, (mx , my+80))
                                rtgd[sps] = [[ix, iy], [ix, iy+1], [ix, iy+2]]
                            elif rtgd[blt]==4 and ix <13 and iy<13:
                                #交叉5
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx+40, my+40))
                                screen.blit(dres.STAR, (mx, my+80))
                                screen.blit(dres.STAR, (mx+80, my))
                                screen.blit(dres.STAR, (mx+80, my+80))
                                rtgd[sps] = [[ix, iy],[ix+1, iy+1],[ix+2, iy],[ix, iy+2],[ix+2, iy+2]]
                            elif rtgd[blt]==5 and ix <11:
                                #横5
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx+40, my))
                                screen.blit(dres.STAR, (mx+80, my))
                                screen.blit(dres.STAR, (mx+120, my))
                                screen.blit(dres.STAR, (mx+160, my))
                                rtgd[sps] = [[ix, iy],[ix+1, iy],[ix+2, iy],[ix+3, iy],[ix+4, iy]]
                            elif rtgd[blt]==6 and iy<11:
                                #竖5
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx, my+40))
                                screen.blit(dres.STAR, (mx, my+80))
                                screen.blit(dres.STAR, (mx, my+120))
                                screen.blit(dres.STAR, (mx, my+160))
                                rtgd[sps] = [[ix, iy],[ix, iy+1],[ix, iy+2],[ix, iy+3],[ix, iy+4]]
                            elif rtgd[blt] == 7 and ix<13 and iy<13:
                                #满圈
                                screen.blit(dres.STAR, (mx, my))
                                screen.blit(dres.STAR, (mx + 40, my + 40))
                                screen.blit(dres.STAR, (mx+40, my))
                                screen.blit(dres.STAR, (mx, my+40))
                                screen.blit(dres.STAR, (mx , my + 80))
                                screen.blit(dres.STAR, (mx+80, my))
                                screen.blit(dres.STAR, (mx + 40, my + 80))
                                screen.blit(dres.STAR, (mx+80, my+40))
                                screen.blit(dres.STAR, (mx + 80, my+80))
                                rtgd[sps] = [[ix, iy], [ix + 1, iy],[ix, iy+1],[ix+2, iy],[ix, iy+2],[ix+1, iy+1],[ix+2, iy+1],[ix+1, iy+2],[ix+2, iy+2]]
                            else:
                                rtgd['iss'] = False
                            #点击开火
                            if is_mouse_down==True and rtgd['iss']==True:
                                sound_kaipao.play(0)
                                #1随机生成对应的炮弹
                                rtgd['lbp']=[]
                                for cpi in rtgd[sps]:
                                    cmx = db.MXS[cpi[0]]
                                    cmy = db.MYS[cpi[1]]
                                    rtgd['lbp'].append([cmx,cmy,cmx+random.randint(-800,800),cmy+random.randint(-500,500),cpi[0],cpi[1]])
                                #2修改状态
                                rtgd['bflying']=True
                                rtgd['scored']=False
        #鼠标最后画
        draw_mouse()
        pygame.display.update()